﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Needle.DevToolkit.Services.UI;
using Needle.DevToolkit.Workspace;
using Needle.DevToolkit.Traits;
using Needle.Utils.Extensions;
using Needle.DevToolkit.UI.Controls;
using System.ComponentModel.Design;

namespace Needle.DevToolkit.UI.ToolWindows
{


    public partial class WorkSpaceExplorer : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        WorkspaceController _controller; 

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public WorkspaceController Controller
        {
            get
            {
                return _controller;
            }
            set
            {
                if (_controller != null)
                    _controller.WorkspaceLoaded -= Initialize;
                _controller = value;
                if (_controller != null)
                    _controller.WorkspaceLoaded += Initialize;
            }
        }

        public WorkSpaceExplorer()
        {
            InitializeComponent();
        }

        private void Initialize(object sender, EventArgs e)
        {
            this.workSpaceTreeView.Nodes.Clear();
            var controller = sender as WorkspaceController;
            if (controller == null)
                return;
            workSpaceTreeView.Nodes.Add(new ItemTreeNode(controller.Workspace));
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            var service = GetService(typeof(ICommandService)) as ICommandService;
            if (service != null)
            {
                var hitInfo = workSpaceTreeView.HitTest(workSpaceTreeView.PointToClient(Control.MousePosition));
                e.Cancel = hitInfo.Node == null;
                if (e.Cancel)
                    return;

                workSpaceTreeView.SelectedNode = hitInfo.Node;

                var verbs = service.GetContextCommands(new Guid(ContextMenuGroup.WorkspaceExplorer), hitInfo.Node.Tag);

                e.Cancel = verbs.Count == 0;
                if (e.Cancel)
                    return;

                contextMenuStrip1.Items.Clear();

                var menuItems = (from verb in verbs
                                 select new ItemVerbToolStripMenuItem(verb)).ToArray();

                menuItems.ForEach(x => x.Tag = hitInfo.Node.Tag);
                contextMenuStrip1.Items.AddRange(menuItems);
            }
        }

        private void workSpaceTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var service = GetService(typeof(ISelectionService)) as ISelectionService;
            if (service != null)
                service.SetSelectedComponents(new object[] { e.Node.Tag });
        }

        private void workSpaceTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var menuService = GetService(typeof(ICommandService)) as ICommandService;

            
        }
    }
}
